package com.lcxw.hxzb.friendcircle.project.system.service.impl;

import com.lcxw.hxzb.friendcircle.common.utils.DateUtils;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdGame;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdGameDistrict;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdGameDistrictMapper;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdGameMapper;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdGameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 陪玩游戏类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-09-15
 */
@Service
public class YwpdGameServiceImpl implements IYwpdGameService
{
    @Autowired
    private YwpdGameMapper ywpdGameMapper;

    @Autowired
    private YwpdGameDistrictMapper ywpdGameDistrictMapper;

    /**
     * 查询陪玩游戏类型
     * 
     * @param gameId 陪玩游戏类型ID
     * @return 陪玩游戏类型
     */
    @Override
    public YwpdGame selectYwpdGameById(Long gameId)
    {
        return ywpdGameMapper.selectYwpdGameById(gameId);
    }

    /**
     * 查询陪玩游戏类型列表
     * 
     * @param ywpdGame 陪玩游戏类型
     * @return 陪玩游戏类型
     */
    @Override
    public List<YwpdGame> selectYwpdGameList(YwpdGame ywpdGame)
    {
        return ywpdGameMapper.selectYwpdGameList(ywpdGame);
    }

    /**
     * 新增陪玩游戏类型
     * 
     * @param ywpdGame 陪玩游戏类型
     * @return 结果
     */
    @Override
    @Transactional
    public int insertYwpdGame(YwpdGame ywpdGame)
    {
        ywpdGame.setCreateTime(DateUtils.getNowDate());
        int flag=ywpdGameMapper.insertYwpdGame(ywpdGame);
        if(flag>0){
            if(!ywpdGame.getDistrictIds().isEmpty()){
                for(Long districtId:ywpdGame.getDistrictIds()){
                    YwpdGameDistrict ywpdGameDistrict=new YwpdGameDistrict();
                    ywpdGameDistrict.setDistrictId(districtId);
                    ywpdGameDistrict.setGameId(ywpdGame.getGameId());
                    ywpdGameDistrictMapper.insertYwpdGameDistrict(ywpdGameDistrict);
                }
            }
        }
        return flag;
    }

    /**
     * 修改陪玩游戏类型
     * 
     * @param ywpdGame 陪玩游戏类型
     * @return 结果
     */
    @Override
    @Transactional
    public int updateYwpdGame(YwpdGame ywpdGame)
    {
        int flag=ywpdGameMapper.updateYwpdGame(ywpdGame);
        ywpdGameDistrictMapper.deleteDistrictByGameId(ywpdGame.getGameId());
        if(flag>0){
            if(!ywpdGame.getDistrictIds().isEmpty()){
                for(Long districtId:ywpdGame.getDistrictIds()){
                    YwpdGameDistrict ywpdGameDistrict=new YwpdGameDistrict();
                    ywpdGameDistrict.setDistrictId(districtId);
                    ywpdGameDistrict.setGameId(ywpdGame.getGameId());
                    ywpdGameDistrictMapper.insertYwpdGameDistrict(ywpdGameDistrict);
                }
            }
        }
        return flag;
    }
    /**
     * 批量删除陪玩游戏类型
     * 
     * @param gameIds 需要删除的陪玩游戏类型ID
     * @return 结果
     */
    @Override
    public int deleteYwpdGameByIds(Long[] gameIds)
    {
        return ywpdGameMapper.deleteYwpdGameByIds(gameIds);
    }

    /**
     * 删除陪玩游戏类型信息
     * 
     * @param gameId 陪玩游戏类型ID
     * @return 结果
     */
    @Override
    public int deleteYwpdGameById(Long gameId)
    {
        return ywpdGameMapper.deleteYwpdGameById(gameId);
    }
}
